package com.xyy.saas.payment.util;

import com.alibaba.dubbo.config.annotation.Reference;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.xyy.saas.leaf.cores.api.LeafApi;
import org.springframework.stereotype.Service;

import java.util.UUID;

/**
 * @author wangtianqi
 * @date 2019/11/28 16:25
 */
@Service
public class LeafUtil {

    private static final org.slf4j.Logger LOGGER = org.slf4j.LoggerFactory.getLogger(LeafUtil.class);

    @Reference(version = "1.0.0")
    private LeafApi leafApi;

    @HystrixCommand(
            fallbackMethod = "getUUID",
            commandProperties = {
                    @HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds", value = "5000"),
                    @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold", value = "2"),
                    @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "1000"),
                    @HystrixProperty(name = "circuitBreaker.errorThresholdPercentage", value = "50"),
                    @HystrixProperty(name = "metrics.rollingStats.timeInMilliseconds", value = "10000"),
                    @HystrixProperty(name = "fallback.isolation.semaphore.maxConcurrentRequests", value = "50"),

            }
    )
    public String getSegmentId(String key) {
        return leafApi.getSegmentId(key);
    }


    @HystrixCommand(
            fallbackMethod = "getUUID",
            commandProperties = {
                    @HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds", value = "5000"),
                    @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold", value = "2"),
                    @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "1000"),
                    @HystrixProperty(name = "circuitBreaker.errorThresholdPercentage", value = "50"),
                    @HystrixProperty(name = "metrics.rollingStats.timeInMilliseconds", value = "10000"),
                    @HystrixProperty(name = "fallback.isolation.semaphore.maxConcurrentRequests", value = "50"),
            }
    )
    public String getSnowflakeId(){
        return leafApi.getSnowflakeId();
    }

    public String getUUID(String key) {
        LOGGER.error("LeafUtil#getUUID execute fallback method");
        try {
            return leafApi.getSegmentId(key);
        }catch (Exception e) {
            LOGGER.error("LeafUtil#getSnowflakeId execute fallback method exception.", e);
        }
        String uuid = UUID.randomUUID().toString();
        return uuid.substring(0, uuid.lastIndexOf("-")).replaceAll("-", "");
    }

    public String getUUID() {
        LOGGER.error("LeafUtil#getUUID execute fallback method");
        try {
            return leafApi.getSnowflakeId();
        }catch (Exception e) {
            LOGGER.error("LeafUtil#getSnowflakeId execute fallback method exception.", e);
        }
        String uuid = UUID.randomUUID().toString();
        return uuid.substring(0, uuid.lastIndexOf("-")).replaceAll("-", "");
    }
}